//
//  NewContactView.m
//  Marketing
//
//  Created by Hanen 3G 01 on 16/5/20.
//  Copyright © 2016年 Hanen 3G 01. All rights reserved.
//

#import "NewContactView.h"
#import "MyTextView.h"


#define InfoCellH 44.0f //每格的高度
#define TitileW 70.0f //每个标题label的宽度
#define STARTX [UIView getWidth:10]

@interface NewContactView ()<UITextFieldDelegate,UITextViewDelegate>
{
    UIView *line22;
}
@end

@implementation NewContactView


- (id)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor whiteColor];
        [self addSubviews];
    }
    return self;
}

- (void)addSubviews
{
    NSArray *   _contactArr = @[@"姓名",@"职务",@"电话",@"邮箱",@"备注"];
    CGFloat tfH    = InfoCellH;
    CGFloat tfW    = KSCreenW - 2 * STARTX - 80.0f;
    
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KSCreenW, 40.0f)];
    view.backgroundColor = graySectionColor;
    [self addSubview:view];
    
    UIImageView *imageView1 = [[UIImageView alloc]initWithFrame:CGRectMake(STARTX, 8, 5, view.height - 16)];
//    imageView1.image = [UIImage imageNamed:@"lanse"];
    [view addSubview:imageView1];
    
    UILabel *basicLabel = [[UILabel alloc]initWithFrame:CGRectMake(STARTX, view.height - 20, 200, 20)];
    basicLabel.text = @"联系人信息";
    [ViewTool setLableFont12:basicLabel];
    
    basicLabel.textColor = blackFontColor;
    [view addSubview:basicLabel];
    
    UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, view.height - 1, KSCreenW , 1)];
    lineView.backgroundColor = grayLineColor;
    [view addSubview:lineView];
    
    for (int i = 0; i<_contactArr.count; i++) {
        UILabel *label = [ViewTool getLabelWith:CGRectMake(STARTX, view.maxY + InfoCellH*i, 80.0f, InfoCellH) WithTitle:_contactArr[i] WithFontSize:13.0f WithTitleColor:lightGrayFontColor WithTextAlignment:NSTextAlignmentLeft];
        [ViewTool setLableFont13:label];
        [self addSubview:label];
        
        UIView *line = [ViewTool getLineViewWith:CGRectMake(STARTX , view.maxY +(InfoCellH -1)*(i+1) , KSCreenW - STARTX, 1) withBackgroudColor:grayLineColor];
        [self addSubview:line];
        
        if (i == _contactArr.count - 1) {
            [line removeFromSuperview];
        }
    }
    
    _nameTF = [self addTextFieldWithFrame:CGRectMake(STARTX + 80.0f, view.maxY , tfW, tfH) AndStr:@"请填写人员" WithTrue:YES];
    [self addSubview:_nameTF];
    
    _businessTF = [self addTextFieldWithFrame:CGRectMake(STARTX + 80.0f, _nameTF.maxY , tfW, tfH) AndStr:@"请填写职务" WithTrue:YES];
    [self addSubview:_businessTF];
    
    _phoneTF = [self addTextFieldWithFrame:CGRectMake(STARTX + 80.0f, _businessTF.maxY , tfW, tfH) AndStr:@"请输入手机(必填)" WithTrue:YES];
    _phoneTF.keyboardType = UIKeyboardTypeNumberPad;
    [self addSubview:_phoneTF];
    
    _emailTF = [self addTextFieldWithFrame:CGRectMake(STARTX + 80.0f, _phoneTF.maxY , tfW, tfH) AndStr:@"请输入邮箱" WithTrue:YES];
    [self addSubview:_emailTF];
   
    _remarkTF = [[MyTextView alloc] initWithFrame:CGRectMake(STARTX + 80.0f - 3, _emailTF.maxY , KSCreenW - 2 * STARTX - 80.0f, [UIView getHeight:60.0f])];
//    _remarkTF.backgroundColor = [UIColor redColor];
    _remarkTF.placehoder = @"请填写备注";
    _remarkTF.delegate = self;
    _remarkTF.textColor = blackFontColor;
    _remarkTF.font = [ViewTool getFont14];
    _remarkTF.placehoderColor = placeholderGrayFontColor;
    [self addSubview:_remarkTF];
    
    line22 = [ViewTool getLineViewWith:CGRectMake(0, _remarkTF.maxY -1, KSCreenW, 1) withBackgroudColor:grayLineColor];
    [self addSubview:line22];
    
    CGRect frame = self.frame;
    frame.size.height = _remarkTF.maxY;
    self.frame = frame;
    
}

-(UITextField *)addTextFieldWithFrame:(CGRect)frame AndStr:(NSString *)placeholder WithTrue:(BOOL)ture{
    
    UITextField *textF=[[UITextField alloc]initWithFrame:frame];
    textF.userInteractionEnabled = ture;
    textF.textColor = blackFontColor;
    textF.placeholder=placeholder;
    textF.delegate = self;
    [textF setValue:placeholderGrayFontColor forKeyPath:@"_placeholderLabel.textColor"];
    [ViewTool setTFPlaceholderFont14:textF];
    return textF;
}

- (void)setTitle:(NSString *)title{
    _title = title;
    CGFloat height = [_title boundingRectWithSize:CGSizeMake(KSCreenW - 2 * STARTX - 80.0f+3, 2000) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingTruncatesLastVisibleLine attributes:@{NSFontAttributeName :[ViewTool getFont14]} context:nil].size.height;
    NSLog(@"%f remark自适应高度+++++++-------",height);
    if (height >[UIView getHeight:60.0f]*2/3) {
        
        _remarkTF.frame = CGRectMake(STARTX + 80.0f - 3, _emailTF.maxY , KSCreenW - 2 * STARTX - 80.0f, height+15+5);
        line22.frame = CGRectMake(0, _remarkTF.maxY -1, KSCreenW, 1);
        CGRect frame = self.frame;
        frame.size.height = _remarkTF.maxY ;
        self.frame = frame;
        
    }else{
        
        line22.frame = CGRectMake(0, _remarkTF.maxY -1, KSCreenW, 1);
        CGRect frame = self.frame;
        frame.size.height = _remarkTF.maxY;
        self.frame = frame;
        
    }
    
}
- (void)textFieldDidEndEditing:(UITextField *)textField{
    if (self.delegate && [self.delegate respondsToSelector:@selector(endEditing)]) {
        [self.delegate endEditing];
    }
}
- (void)textViewDidEndEditing:(UITextView *)textView{
    if (self.delegate && [self.delegate respondsToSelector:@selector(endEditing)]) {
        [self.delegate endEditing];
    }
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
